Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
4.6 KiB

  1. namespace VRTK.Examples
  2. {
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class PanelMenuSaucerGrid : MonoBehaviour
  6. {
  7. public GridLayoutGroup gridLayoutGroup;
  8. public MeshRenderer changeObject;
  9. public VRTK_PanelMenuItemController panelMenuController;
  10. public Color[] colours = new Color[0];
  11. protected int currentIndex = 0;
  12. protected readonly Color colorDefault = Color.white;
  13. protected readonly Color colorSelected = Color.green;
  14. protected readonly float colorAlpha = 0.25f;
  15. protected enum Direction
  16. {
  17. None,
  18. Up,
  19. Down,
  20. Left,
  21. Right
  22. }
  23. protected virtual void OnEnable()
  24. {
  25. if (panelMenuController != null)
  26. {
  27. panelMenuController.PanelMenuItemSwipeTop += PanelMenuItemSwipeTop;
  28. panelMenuController.PanelMenuItemSwipeBottom += PanelMenuItemSwipeBottom;
  29. panelMenuController.PanelMenuItemSwipeLeft += PanelMenuItemSwipeLeft;
  30. panelMenuController.PanelMenuItemSwipeRight += PanelMenuItemSwipeRight;
  31. panelMenuController.PanelMenuItemTriggerPressed += PanelMenuItemTriggerPressed;
  32. }
  33. SetGridLayoutItemSelectedState(currentIndex);
  34. }
  35. protected virtual void PanelMenuItemTriggerPressed(object sender, PanelMenuItemControllerEventArgs e)
  36. {
  37. if (currentIndex < colours.Length && changeObject != null)
  38. {
  39. changeObject.material.color = colours[currentIndex];
  40. }
  41. }
  42. protected virtual void PanelMenuItemSwipeRight(object sender, PanelMenuItemControllerEventArgs e)
  43. {
  44. MoveSelectGridLayoutItem(Direction.Right);
  45. }
  46. protected virtual void PanelMenuItemSwipeLeft(object sender, PanelMenuItemControllerEventArgs e)
  47. {
  48. MoveSelectGridLayoutItem(Direction.Left);
  49. }
  50. protected virtual void PanelMenuItemSwipeBottom(object sender, PanelMenuItemControllerEventArgs e)
  51. {
  52. MoveSelectGridLayoutItem(Direction.Down);
  53. }
  54. protected virtual void PanelMenuItemSwipeTop(object sender, PanelMenuItemControllerEventArgs e)
  55. {
  56. MoveSelectGridLayoutItem(Direction.Up);
  57. }
  58. protected virtual void SetGridLayoutItemSelectedState(int index)
  59. {
  60. foreach (Transform childTransform in gridLayoutGroup.transform)
  61. {
  62. GameObject child = childTransform.gameObject;
  63. if (child != null)
  64. {
  65. Color color = colorDefault;
  66. color.a = colorAlpha;
  67. child.GetComponent<Image>().color = color;
  68. }
  69. }
  70. Transform selected = gridLayoutGroup.transform.GetChild(index);
  71. if (selected != null)
  72. {
  73. Color color = colorSelected;
  74. color.a = colorAlpha;
  75. selected.GetComponent<Image>().color = color;
  76. }
  77. }
  78. protected virtual bool MoveSelectGridLayoutItem(Direction direction)
  79. {
  80. int newIndex = FindNextItemBasedOnMoveDirection(direction);
  81. if (newIndex != currentIndex)
  82. {
  83. SetGridLayoutItemSelectedState(newIndex);
  84. currentIndex = newIndex;
  85. }
  86. return true;
  87. }
  88. protected virtual int FindNextItemBasedOnMoveDirection(Direction direction)
  89. {
  90. float width = gridLayoutGroup.preferredWidth;
  91. float cellWidth = gridLayoutGroup.cellSize.x;
  92. float spacing = gridLayoutGroup.spacing.x;
  93. int cellsAccross = (int)Mathf.Floor(width / (cellWidth + (spacing / 2))); // quick / dirty
  94. int childCount = gridLayoutGroup.transform.childCount;
  95. switch (direction)
  96. {
  97. case Direction.Up:
  98. int nextUp = currentIndex - cellsAccross;
  99. return (nextUp >= 0) ? nextUp : currentIndex;
  100. case Direction.Down:
  101. int nextDown = currentIndex + cellsAccross;
  102. return (nextDown < childCount) ? nextDown : currentIndex;
  103. case Direction.Left:
  104. int nextLeft = currentIndex - 1;
  105. return (nextLeft >= 0) ? nextLeft : currentIndex;
  106. case Direction.Right:
  107. int nextRight = currentIndex + 1;
  108. return (nextRight < childCount) ? nextRight : currentIndex;
  109. default:
  110. return currentIndex;
  111. }
  112. }
  113. }
  114. }